<?php
declare (strict_types=1);

namespace app\middleware;

use Closure;
use think\response\Redirect;

/**
 * 后台中间件
 * User : Du
 * Date : 2022/11/27
 * Time : 17:23
 * Class AdminCheck
 * @package projects\middleware
 */
class AdminCheck
{
    /**
     * 登录校验
     *
     * @param $request
     * @param Closure $next
     * @return mixed|Redirect
     * @author Du
     * @date 2021/3/31 10:49
     */
    public function handle($request, Closure $next)
    {
        //用户验证  保证唯一登录
        if (is_array(session('manage_info'))) {
            $res = session('manage_info');
            $cache = cache('Manage_' . $res['id']);
            if ($res['login_time'] === $cache['login_time']) {
                return $next($request);
            }
            session('manage_info', null);
        }
        if ($request->method() === 'POST') {
            $res = [
                'code' => 99,
                'msg' => '登录信息已过期',
            ];
            return json($res);
        }
        return redirect('/');
    }
}
